package com.leyou.item.service;

import com.leyou.common.advice.CommonExceptionHandler;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LeYouException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 *  商品种类
 *  @author Rose
 *  @date 2020/1/8
 * create by Rose on 2020/1/8
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper ;


    /**
     *
     *
     * @param pid
     * @return
     */
    public List<Category> queryCategoryByPid(Long pid) {
        // 查询条件， mapper会把对象中的非空属性作为 查询条件
        Category t = new Category();
        t.setParentId(pid);
        List<Category> list = this.categoryMapper.select(t);
        // 判断结果
        if(CollectionUtils.isEmpty(list)){
            throw new LeYouException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return list;
    }


    /**
     *  通过品牌id 查询商品分类
     *
     * @param bid
     * @return
     */
    public List<Category> queryByBrandId(Long bid) {

        List<Category>  list = this.categoryMapper.queryByBrandId(bid);
        // 判断结果
        if(CollectionUtils.isEmpty(list)){
            throw new LeYouException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return list;
    }

    /**
     *
     *
     * @param ids
     * @return
     */
    public List<Category> queryByIds(List<Long> ids ){
        List<Category> categoryList = categoryMapper.selectByIdList(ids);
        return categoryList;
    }
}
